<template>
    <view class="messag-content">
        <block v-for="item in chatList" :key="item._id">
            <!-- 管理员 -->
            <view class="msg-left message" v-if="item.messageType === '001'">
                <image :src="item.avatar" mode="aspectFill" />
                <text>{{ item.message }}</text>
            </view>
            <!-- 用户 -->
            <view class="msg-right message" v-else>
                <text>{{ item.message }}</text>
                <image :src="item.avatar" mode="aspectFill" />
            </view>
        </block>
    </view>
    <view class="Input_field">
        <input type="text" :maxlength="-1" :cursor-spacing="40" confirm-type="发送" v-model="userMessage"
            @confirm="sendMessage" />
        <image src="/static/fasong.png" mode="aspectFit" @tap="sendMessage" />
    </view>
</template>
<script setup lang="ts">
import { requestUrl } from "@/api/request";
import type { chatMessage } from "@/types";
import { onShow } from "@dcloudio/uni-app";
import { ref } from "vue";
const io = require("../../api/weapp.socket.io.js");
const userMessage = ref('')
const socket = ref<any>();
const chatList = ref<chatMessage[]>([])
const userInfo = uni.getStorageSync("userInfo")
onShow(() => {
    socket.value = io(requestUrl, {
        transports: ['websocket'],
        query: {
            token: userInfo?.token,
            clientType: 'USER',
        },
        reconnectionAttempts: 4,//失败后重新连接次数，一直失败总共尝试四次
        reconnectionDelay: 2000,//重新连接间隔时间毫秒
    })

    socket.value.on("connect", () => {
        console.log("连接成功");

    })
    socket.value.on("reconnect_failed", () => {
        console.log("重新连接失败");

    })
    socket.value.on("reconnect_attempt", () => {
        console.log("正在重新连接");

    })
    socket.value.on("error", (err: any) => {
        if (err == "401") {
            uni.navigateTo({ url: "/pages/login/index" })
        }
        console.log("连接失败");

    })
    socket.value.on("wxChat", (data: chatMessage) => {
        console.log("接收到数据：", data);
        chatList.value?.push(data);
    })

})

const sendMessage = () => {
    if (userMessage.value.trim()) {
        const data = {
            avatar: userInfo.avatar,
            message: userMessage.value,
            messageType: '002',
            nickname: userInfo.nickname,
            _id: userInfo._id
        }
        socket.value.emit("userMessage", data)
        chatList.value.push(data)
        userMessage.value = '';
        uni.pageScrollTo({scrollTop:3000})
    }


}

</script>
<style>
page {
    background-color: #eaeaea;
    padding-bottom: 300rpx;
}

.messag-content {
    display: flex;
    flex-direction: column;
    padding: 20rpx;
}

.message {
    display: flex;
    align-items: center;
    margin: 20rpx 0;
}

.message image {
    width: 65rpx;
    height: 65rpx;
    border-radius: 10rpx;
}

.message text {
    border-radius: 10rpx;
    padding: 10rpx;
}

.msg-right {
    align-self: flex-end;
}

.msg-left {
    padding-right: 150rpx;
}

.msg-right {
    padding-left: 150rpx;
}

.msg-left text {
    background-color: #ffffff;
    margin-left: 10rpx;
}

.msg-right text {
    background-color: #304bcc;
    color: #ffffff;
    margin-right: 10rpx;
}

/* 固定在底部的输入框样式 */
.Input_field {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    border-top: 1px solid #eee;
    padding: 10rpx 20rpx 70rpx 20rpx;
    background-color: #f6f6f6;
}

/* 固定在底部的输入框input样式 */
.Input_field input {
    flex: 1;
    background-color: #fff;
    border-radius: 10rpx;
    padding: 20rpx;
}

/* 固定在底部的输入框左右两边清空和发送样式 */
.Input_field image {
    height: 70rpx;
    width: 70rpx;
    margin-left: 20rpx;
}
</style>